i = 0
nn.results <- list()
set.seed(123)
for (year in start.year:end.year) {
  i = i + 1
  print(year)
  # Fit NN
  nn.results[[i]] <- list()
  dta.past <- dta[dta[,t.var]< year,]
 
  model <- keras_model_sequential() 
  model %>% 
    layer_dense(units = 256, activation = 'relu', input_shape = length(rhs)) %>% 
    layer_dropout(rate = .5) %>% 
    layer_dense(units = 128, activation = 'relu') %>%
    layer_dropout(rate = .3) %>%
    layer_dense(units = 2, activation = 'sigmoid')
  
  model %>% compile(
    loss = 'binary_crossentropy',
    optimizer = 'adam',
    metrics = c('accuracy')
  )
  set.seed(i)
  history <- model %>% fit(
    as.matrix(dta.past[rhs]), to_categorical(dta.past[,dv]), 
    epochs = 20, batch_size = 256, 
    validation_split = 0.2
  )
  hours <- floor((proc.time()[3]-start.time)/3600)
  mins <- floor((proc.time()[3]-start.time)/60) - hours*60
  secs <- floor((proc.time()[3]-start.time)) - hours*3600 - mins*60
  
  print(paste(hours,
              "h",
              mins,
              "m",
              secs,
              "s elapsed",
              sep = ""))
 

  nn.results[[i]]$model <- model
  nn.results[[i]]$fit.oos <- (model %>% predict_proba(as.matrix(dta[dta[,t.var]== year,rhs])))[,2]
  
  nn.results[[i]]$actual.oos <- as.matrix(dta[dta[,t.var] == year,
                                              dv])
  print(year)
}
save(nn.results,file=paste(modeldir,"/",
                              country,
                              "_nn5_",
                              v,
                              "_",
                              rhs.group,
                              ".RData",
                              sep = ""))